package com.idbk.chargestation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;

import com.idbk.chargestation.R;
import com.idbk.chargestation.activity.pile.ActivityPointDetail;
import com.idbk.chargestation.adapter.PointListAdapter;
import com.idbk.chargestation.bean.JsonPointsOverview;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.MapUtil;

/**
 * @author lupc
 */
public class DialogPointInfo extends Dialog implements View.OnClickListener {

    private JsonPointsOverview mCurrentPoint;
    private FrameLayout mLayoutPointInfo;
    private PointListAdapter.PointInfoHold mPointInfoHold;
    private Context mContext;

    public DialogPointInfo(Context context, JsonPointsOverview data) {
        super(context);
        mContext = context;
        mCurrentPoint = data;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (getWindow() != null) {
            //设置dialog默认背景为透明
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            getWindow().setWindowAnimations(R.style.share_dialog);//设置显示动画
            getWindow().setGravity(Gravity.BOTTOM);//设置Dialog显示在下方
            //设置灰度
            getWindow().setDimAmount(0.0f);
        }


        LayoutInflater inflater = LayoutInflater.from(getContext());
        mLayoutPointInfo = (FrameLayout) inflater.inflate(R.layout.layout_point_info, null);//dialog布局显示
        setContentView(mLayoutPointInfo);
        //设置Dialog横向全屏,纵向匹配内容
        getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        setupView();

        setupData();
    }

    private void setupView() {
        //导航
        mLayoutPointInfo.findViewById(R.id.layout_navi).setOnClickListener(this);
        //详情
        mLayoutPointInfo.findViewById(R.id.layout_detail).setOnClickListener(this);
        mLayoutPointInfo.findViewById(R.id.layout_pile).setOnClickListener(this);
        mLayoutPointInfo.findViewById(R.id.layout_comment).setOnClickListener(this);

        mPointInfoHold = new PointListAdapter.PointInfoHold();
        mPointInfoHold.initView(mLayoutPointInfo);
    }

    private void setupData() {
        mPointInfoHold.fillData(mContext, mCurrentPoint);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_navi:
                if (mCurrentPoint != null) {
                    MapUtil.startNavi(getContext(), mCurrentPoint.lat, mCurrentPoint.lng);
                }
                break;
            case R.id.layout_detail:
                startPointInfoActivity(ActivityPointDetail.POINT_DETAIL);
                break;
            case R.id.layout_pile:
                startPointInfoActivity(ActivityPointDetail.POINT_PILES);
                break;
            case R.id.layout_comment:
                startPointInfoActivity(ActivityPointDetail.POINT_COMMENT);
                break;
            default:
                break;
        }
    }

    //点击进入详细信息
    private void startPointInfoActivity(int index) {
        if (mCurrentPoint == null) {
            return;
        }
        Intent intent = new Intent(getContext(), ActivityPointDetail.class);
        intent.putExtra(Const.KEY_ID, mCurrentPoint.id);
        intent.putExtra(Const.POINT_DETAIL_INDEX, index);
        getContext().startActivity(intent);
    }

}
